/*
 * Group.java
 *
 * Created on July 24, 2006, 9:18 AM
 *
 * @author Francois Daniels
 */

package za.gateway.container;

/**
 * This class will encaptulate the details regarding a group on our 
 * system. It will be used by the Groups class.
 *
 * @author Francois Daniels
 */
public class Group {
    /** Variables belonging to the Group class */
    
    /** 
     * This is the name of the group. This name uniquely identifies a 
     * group, thus no two groups will have the same name.
     */
    protected String Name;
    
    /** 
     * This is the password of the group. It is used to verify that 
     * someone belongs to this group.
     */
    protected String Password;
    
    /** 
     * This is the password of the group. It is used to verify that 
     * someone belongs to this group.
     */
    protected String [] ParentGroups;
    
    /** 
     * This is the description of the group. This is just used to store 
     * notes or any other information about a particular group. Its 
     * serves no purpose other than for informational completeness.
     */
    protected String Description;
    
    /** 
     * This is set to true when this object has been modified since 
     * creation.
     */
    protected boolean Dirty;
    
    
    
    /** Functions belonging to the Group class */
    
    /** Creates a new instance of Group
     * This constructor just initialises all the data to the values 
     * supplied by the arguments.
     *
     * @param NameData          The name of the group.
     * @param PasswordData      The password of the group.
     * @param ParentGroupsData  The groups which are parents to this 
     *                          group. It may be null.
     * @param DescriptionData   The description of the group. It may be
     *                          null.
     */
    public Group (  String NameData, String PasswordData, 
            String [] ParentGroupsData, String DescriptionData) {
        Name            = NameData;
        Password        = PasswordData;
        Description     = DescriptionData;
        Dirty           = false;
        
        if (ParentGroupsData == null) {
            ParentGroups    = new String [0];
        } else {
            ParentGroups    = new String [ParentGroupsData.length];
            System.arraycopy (ParentGroupsData, 0, ParentGroups, 0,
                    ParentGroupsData.length);
        }
    }
    
    /** 
     * This function is used to update the details of a Group.
     *
     * @param NameData          The name of the group.
     * @param PasswordData      The password of the group.
     * @param ParentGroupsData  The groups which are parents to this 
     *                          group. It may be null.
     * @param DescriptionData   The description of the group. It may be 
     *                          null.
     */
    public void SetData  (  String NameData, String PasswordData, 
            String [] ParentGroupsData, String DescriptionData) {
        Name            = NameData;
        Password        = PasswordData;
        //ParentGroups    = ParentGroupsData;
        Description     = DescriptionData;
        Dirty           = true;
        
        if (ParentGroupsData == null) {
            ParentGroups    = new String [0];
        } else {
            ParentGroups    = new String [ParentGroupsData.length];
            System.arraycopy (ParentGroupsData, 0, ParentGroups, 0,
                    ParentGroupsData.length);
        }
    }
    
    /**
     * Calling this function sets this object to 'dirty'. This means we
     * need to save it has changed since creation and needs to be saved 
     * to the database
     */
    public void SetDirty () {
        Dirty = true;
    }
    
    /**
     * Returns the name of the group.
     *
     * @return The name of the group.
     */
    public String GetName () {
       return Name; 
    }
    
    /**
     * Returns the password of the group.
     *
     * @return The password of the group.
     */
    public String GetPassword () {
        return Password;
    }
    
    /**
     * Returns the names of the parent groups.
     *
     * @return The names of the parent groups.
     */
    public String [] GetParentGroups () {
        return ParentGroups;
    }
    
    /**
     * Returns the description of the group.
     *
     * @return The description of the group.
     */
    public String GetDescription () {
        return Description;
    }
    
    /**
     * Compares two object. If the second object is not of type Group 
     * then we return false. If they have the same name, then they are 
     * equal.
     *
     * @param rhs   The object on which we will test for equality.
     * @return      true if they share the same Name.
     */
    public boolean equals (Object rhs) {
        if (rhs == null) return false;
        
        try {
            return (Name == null)?false:
                Name.equals (((Group)rhs).GetName ());
        } catch (ClassCastException e) {
            return false;
        }
    }
    
    /**
     * This function will determine whether or not this object has been
     * modified since it has been created.
     *
     * @return true if it has been modified since creation, false 
     * otherwise.
     **/
    public boolean IsDirty () {
        return Dirty;
    }
}
